package battle 
{
	import Base.Net.NetSocket;
	
	import Common.GameDefine;
	import Common.GameNetEvent;
	import Common.GameNetManager;
	
	import battle.BattleData;
	
	import flash.events.MouseEvent;
	
	import Common.MapDirection;

	/**
	 * ...
	 * @author 
	 */
	public class BattleHandlerCommand extends BattleHandlerInterface
	{
		
		public function BattleHandlerCommand() 
		{
			
		}
		
		override public function init():void 
		{
			super.init();
		}
		
		override public function clear():void 
		{
			super.clear();
		}
		
		override public function active():void 
		{
			BattleData.instance.fightMC.visible = true;
			BattleData.instance.fightMC.startTimer();
			
			for (var i:int = BattleData.instance.playerArray.length - 1 ; i >= 0 ; i-- ) 
			{
				BattleData.instance.playerArray[ i ].setDirection( MapDirection.NORTH );
			}
			for ( i = BattleData.instance.playerPetArray.length - 1 ; i >= 0 ; i-- ) 
			{
				BattleData.instance.playerPetArray[ i ].setDirection( MapDirection.NORTH );
			}
			
			for ( i = BattleData.instance.defenderArray.length - 1 ; i >= 0 ; i-- ) 
			{
				BattleData.instance.defenderArray[ i ].setDirection( MapDirection.SOUTH );
			}
			for ( i = BattleData.instance.defenderPetArray.length - 1 ; i >= 0 ; i-- ) 
			{
				BattleData.instance.defenderPetArray[ i ].setDirection( MapDirection.SOUTH );
			}
		}
		
		override public function onClick( e:MouseEvent ):void 
		{
			//if ( data.fightMC.getTime() > 29 ) 
			//{
			//	return;
			//}
			
			if ( BattleData.instance.fightMC.playerCommandMC.defenceButton == e.target )
			{
				BattleData.instance.battleCommand.type = BattleCommand.DEFENCE;
				
				sendCommand();
				
				BattleData.instance.fightMC.setState( BattleState.WAIT );
			}

			if ( BattleData.instance.fightMC.playerCommandMC.escapeButton == e.target )
			{
				BattleData.instance.battleCommand.type = BattleCommand.ESCAPE;
				trace(  BattleData.instance.battleCommand.type );
				sendCommand();
				
				BattleData.instance.fightMC.setState( BattleState.WAIT );
			}
			
			if ( BattleData.instance.fightMC.playerCommandMC.attackDown.visible ) 
			{
				// attack target
				var target:int = GameDefine.INVALIDID;
				
				BattleData.instance.battleCommand.type = BattleCommand.ATTACK;
				BattleData.instance.battleCommand.targetType = e.target.parent.type;
				BattleData.instance.battleCommand.target = e.target.parent.index;
				
				sendCommand();
				
				BattleData.instance.fightMC.setState( BattleState.WAIT );
			}
			
			//super.onClick(e);
		}
		
		override public function onEvent( e:MouseEvent ):void 
		{
			//super.onEvent(e);
		}
		
		override public function update( delay:Number ):int 
		{
			//for (var i:int = 0; i < playerArray.length; i++) 
			//{
				//playerArray[i].update( delay );
			//}
			//
			//for (var j:int = 0; j < enemyArray.length; j++) 
			//{
				//enemyArray[j].update( delay );
			//}
			
			super.update( delay );
			
			return GameDefine.INVALIDID;
		}
		
		public function sendCommand():void
		{
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
			
			netSocket.writeShort( GameNetEvent.SEND_BATTLE_COMMAND_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_BATTLE_COMMAND_ID );
			
			
			netSocket.writeByte( BattleData.instance.battleCommand.type );
			netSocket.writeByte( BattleData.instance.battleCommand.targetType );
			netSocket.writeByte( BattleData.instance.battleCommand.target );
			netSocket.writeByte( -1 );
			netSocket.writeByte( BattleData.instance.battleCommand.type );
			netSocket.writeByte( BattleData.instance.battleCommand.targetType );
			netSocket.writeByte( BattleData.instance.battleCommand.target );
			netSocket.writeByte( -1 );
			
			netSocket.Send();
			
			
		}
		
	}

}